package com.wangxiang.shop.order.service.Impl;

import cn.hutool.core.util.NumberUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangxiang.common.utils.*;
import com.wangxiang.shop.order.dao.ReceiptOrderDao;
import com.wangxiang.shop.order.dao.UnsubscribeOrderDao;
import com.wangxiang.shop.order.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wangxiang.shop.order.dao.MemberOrderDao;
import com.wangxiang.shop.order.service.MemberOrderService;


@Service("memberOrderService")
public class MemberOrderServiceImpl extends ServiceImpl<MemberOrderDao, MemberOrderEntity> implements MemberOrderService {
    @Autowired
    private MemberOrderDao memberOrderDao;
    @Autowired
    private ReceiptOrderDao receiptOrderDao;
    @Autowired
    private UnsubscribeOrderDao unsubscribeOrderDao;

    @Override
    public List<Integer> addByCart(List<ShopCartEntity> list) {
        List<Integer> cartIdList = new ArrayList<>();
        for (ShopCartEntity shopCartEntity : list) {
//            System.out.println(shopCartEntity);
            MemberOrderEntity memberOrderEntity = new MemberOrderEntity();
            memberOrderEntity.setSpuId(shopCartEntity.getSpuId());
            memberOrderEntity.setSkuId(shopCartEntity.getSkuId());
            memberOrderEntity.setUserId(shopCartEntity.getUserId());
            memberOrderEntity.setNum(shopCartEntity.getNum());
            memberOrderEntity.setCartId(shopCartEntity.getCartId());
            memberOrderEntity.setReceiptId(shopCartEntity.getReceiptId());
            memberOrderEntity.setTime(new Date());
            //计算价格
            double mul = NumberUtil.mul(shopCartEntity.getNum(), shopCartEntity.getPrice());
            memberOrderEntity.setPrice(mul);
            memberOrderEntity.setStatus(0);
            int insert = memberOrderDao.insert(memberOrderEntity);
            if (insert>0){
                cartIdList.add(shopCartEntity.getCartId());
            }

        }
        return cartIdList;

    }

    @Override
    public PageEntity page(PageEntity page) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<MemberOrderEntity> memberOrderEntities = memberOrderDao.selectList(null);
        for (MemberOrderEntity memberOrderEntity : memberOrderEntities) {
            TimeUtils timeUtils = new TimeUtils();
            String time = timeUtils.timeChange(memberOrderEntity.getTime());
            memberOrderEntity.setTimeStr(time);
        }
        PageInfo<MemberOrderEntity> memberOrderEntityPageInfo = new PageInfo<>(memberOrderEntities);
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(memberOrderEntityPageInfo);
        return generate;
    }

    @Override
    public List<HashMap<String, Integer>> list(int userId, int typeId) {
        //typeId参数 0 全部 1代发货 2待收货 3待评价，历史订单
        List<MemberOrderEntity> orders = memberOrderDao.listByUserId(userId);
        //全部
        if (typeId==0){
          List<HashMap<String, Integer>> hashList = new ArrayList<>();
            for (MemberOrderEntity order : orders) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("userId",userId);
                map.put("orderId",order.getOrderId());
                map.put("spuId",order.getSpuId());
                map.put("skuId",order.getSkuId());
                hashList.add(map);
            }
            return hashList;
        }
        //代发货
        if (typeId==1){
            List<HashMap<String, Integer>> hashList = new ArrayList<>();
            for (MemberOrderEntity order : orders) {
               if (order.getStatus()==0){
                   HashMap<String, Integer> map = new HashMap<>();
                   map.put("userId",userId);
                   map.put("orderId",order.getOrderId());
                   map.put("spuId",order.getSpuId());
                   map.put("skuId",order.getSkuId());
                   hashList.add(map);
               }
            }
            return hashList;
        }
        //已发货，代签收
        if (typeId==2){
            List<HashMap<String, Integer>> hashList = new ArrayList<>();
            for (MemberOrderEntity order : orders) {
                if (order.getStatus()==1){
                    HashMap<String, Integer> map = new HashMap<>();
                    map.put("userId",userId);
                    map.put("orderId",order.getOrderId());
                    map.put("spuId",order.getSpuId());
                    map.put("skuId",order.getSkuId());
                    hashList.add(map);
                }
            }
            return hashList;
        }
        //退订，表3,4,5
        if (typeId==3){
            List<HashMap<String, Integer>> hashList = new ArrayList<>();
            for (MemberOrderEntity order : orders) {
                if (order.getStatus()==3|order.getStatus()==4|order.getStatus()==5){
                    HashMap<String, Integer> map = new HashMap<>();
                    map.put("userId",userId);
                    map.put("orderId",order.getOrderId());
                    map.put("spuId",order.getSpuId());
                    map.put("skuId",order.getSkuId());
                    hashList.add(map);
                }
            }
            return hashList;
        }
        //历史订单 表2 ，4 ，5
        if (typeId==4){
            List<HashMap<String, Integer>> hashList = new ArrayList<>();
            for (MemberOrderEntity order : orders) {
                if (order.getStatus()==2|order.getStatus()==4|order.getStatus()==5){
                    HashMap<String, Integer> map = new HashMap<>();
                    map.put("userId",userId);
                    map.put("orderId",order.getOrderId());
                    map.put("spuId",order.getSpuId());
                    map.put("skuId",order.getSkuId());
                    hashList.add(map);
                }
            }
            return hashList;
        }
        return null;
    }

    @Override
    public List<DevOrderEntity> orderList(List<HashMap<String, String>> list) {
        ArrayList<DevOrderEntity> devOrderEntities = new ArrayList<>();
        for (HashMap<String, String> listMap : list) {
            DevOrderEntity devOrderEntity = new DevOrderEntity();
            //查订单
            int orderId = Integer.valueOf(listMap.get("orderId"));
            MemberOrderEntity memberOrderEntity = memberOrderDao.selectById(orderId);
            devOrderEntity.setOrderId(memberOrderEntity.getOrderId());
            devOrderEntity.setName(listMap.get("name"));
            devOrderEntity.setStore(listMap.get("store"));
            devOrderEntity.setStyle(listMap.get("style"));
            devOrderEntity.setSize(listMap.get("size"));
            devOrderEntity.setImage(listMap.get("image"));
            devOrderEntity.setRemark(memberOrderEntity.getRemark());
            devOrderEntity.setStatus(memberOrderEntity.getStatus());
            devOrderEntity.setPrice(memberOrderEntity.getPrice());
            devOrderEntity.setNum(memberOrderEntity.getNum());
            devOrderEntities.add(devOrderEntity);
        }
        return devOrderEntities;
    }

    @Override
    public HashMap<String, Integer> typeNumList(int userId) {
        List<MemberOrderEntity> memberOrderEntities = memberOrderDao.listByUserId(userId);
        HashMap<String, Integer> map = new HashMap<>();
        int type0 = 0;
        int type1 = 0;
        int type2 = 0;
        int type3 = 0;
        for (int i = 0; i < memberOrderEntities.size(); i++) {
            //待发货
            if (memberOrderEntities.get(i).getStatus()==0){
                type0++;
            }
            //已发货
            if (memberOrderEntities.get(i).getStatus()==1){
                type1++;
            }
            //已签收
            if (memberOrderEntities.get(i).getStatus()==2){
                type2++;
            }
            //退货
            if (memberOrderEntities.get(i).getStatus()==3){
                type3++;
            }

        }
        map.put("type0",type0);
        map.put("type1",type1);
        map.put("type2",type2);
        map.put("type3",type3);

        return map;
    }
    //退订，退单
    @Override
    public int outGoods(DevOrderEntity devOrder) {
        int i = memberOrderDao.updateStatus(devOrder.getOrderId(), 3);
        if (i>0){
            int i1 = memberOrderDao.updateRemark(devOrder.getOrderId(), devOrder.getRemark());
            if (i1>0){
                int i2 = memberOrderDao.updateExplanation(devOrder.getOrderId(), devOrder.getExplanation());
                return i2;
            }

        }
        return 0;
    }

    @Override
    public int addByPayNow(HashMap<String, Object> map) {
//        System.out.println("服务层"+map);
        int userId = Integer.valueOf(map.get("userId").toString());
        int spuId = Integer.valueOf(map.get("spuId").toString());
        int skuId = Integer.valueOf(map.get("skuId").toString());
        int num = Integer.valueOf(map.get("num").toString());
        double price = Double.valueOf(map.get("price").toString());
        int receipt = Integer.valueOf(map.get("receiptId").toString());
        String image = String.valueOf(map.get("image"));
        MemberOrderEntity entity = new MemberOrderEntity();
        entity.setUserId(userId);
        entity.setSpuId(spuId);
        entity.setSkuId(skuId);
        entity.setNum(num);
        entity.setPrice(price);
        entity.setImage(image);
        entity.setReceiptId(receipt);
        entity.setTime(new Date());
        entity.setStatus(0);
        entity.setCartId(0);
        int insert = memberOrderDao.insert(entity);


        return insert;
    }

    @Override
    public List<HashMap<String, Object>> pageList(List<MemberOrderEntity> list) {
        ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
        list.forEach((item)->{
            HashMap<String, Object> map = new HashMap<>();
            map.put("orderId",item.getOrderId());
            map.put("userId",item.getUserId());
            map.put("cartId",item.getCartId());
            String timeStr = item.getTimeStr();
            Object shipTime = timeStr;
            map.put("shipTime", shipTime);
            map.put("spuId",item.getSpuId());
            map.put("skuId",item.getSkuId());
            map.put("orderStatus",item.getStatus());
            map.put("num",item.getNum());
            map.put("price",item.getPrice());
            map.put("receiptId",item.getReceiptId());
            hashMaps.add(map);


        });
        return hashMaps;
    }

    @Override
    public int receiptGoods(HashMap<String, Integer> map) {
        int orderId = map.get("orderId");
        //更新全局表状态
        int i = memberOrderDao.updateStatus(orderId, 2);// 2已签收
        if (i>0){
            //收货日志表
            ReceiptOrderEntity receiptOrderEntity = new ReceiptOrderEntity();
            receiptOrderEntity.setOrderId(orderId);
            receiptOrderEntity.setTime(new Date());
            int insert = receiptOrderDao.insert(receiptOrderEntity);

                return insert;


        }else {
            return 0;
        }

    }

    @Override
    public int auditUnsubscribeApply(HashMap<String, Object> map) {
        int orderId = Integer.valueOf(map.get("orderId").toString());
        int auditUserId = Integer.valueOf(map.get("userId").toString());
        int status = Integer.valueOf(map.get("status").toString());
        String remark = null;
        if (map.get("remark")!=null){
            String remark1 = map.get("remark").toString();
            remark = remark1;
        }
        MemberOrderEntity entity = memberOrderDao.selectById(orderId);


        //跟新状态
        if (status == 0) {
            //成功退订
            //更新状态
            int i = memberOrderDao.updateStatus(orderId, 4);
            if (i>0){
                //更新备注
//                int i1 = memberOrderDao.upadateUnsubscribeApply(orderId, remark);

                    //插入日志表
                    UnsubscribeOrderEntity unsubscribeOrderEntity = new UnsubscribeOrderEntity();
                    unsubscribeOrderEntity.setOrderId(orderId);
                    unsubscribeOrderEntity.setApplyBc(entity.getRemark());
                    unsubscribeOrderEntity.setRpRemark(entity.getExplanation()); //用户补充理
                    unsubscribeOrderEntity.setTime(new Date());
                    unsubscribeOrderEntity.setStatus(0);  //通过
                    unsubscribeOrderEntity.setDoUserId(auditUserId);
                    int insert = unsubscribeOrderDao.insert(unsubscribeOrderEntity);
                    return insert;

            }

        }
        if (status == 1){
            //不批准退订

            //更新状态
            int i = memberOrderDao.updateStatus(orderId, 4);
            if (i>0){
                //更新备注
                int i1 = memberOrderDao.upadateUnsubscribeApply(orderId, remark);
                if (i1>0){
                    //插入日志表
                    UnsubscribeOrderEntity unsubscribeOrderEntity = new UnsubscribeOrderEntity();
                    unsubscribeOrderEntity.setOrderId(orderId);
                    unsubscribeOrderEntity.setApplyBc(entity.getRemark());
                    unsubscribeOrderEntity.setRpRemark(entity.getExplanation()); //用户补充理由
                    unsubscribeOrderEntity.setRemark(remark);
                    unsubscribeOrderEntity.setTime(new Date());
                    unsubscribeOrderEntity.setStatus(1);  //通过
                    unsubscribeOrderEntity.setDoUserId(auditUserId);
                    int insert = unsubscribeOrderDao.insert(unsubscribeOrderEntity);
                    return insert;
                }

            }

        }

        return 0;

    }

    @Override
    public PageEntity pageByUser(HashMap<String, Object> map ,int userId) {
        int pageNum = Integer.valueOf(map.get("pageNum").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        PageHelper.startPage(pageNum,pageSize);
        List<MemberOrderEntity> memberOrderEntities = memberOrderDao.listByUserId(userId);
        memberOrderEntities.forEach((item)->{
            TimeUtils timeUtils = new TimeUtils();
            String s = timeUtils.timeChange(item.getTime());
            item.setTimeStr(s);
        });

        PageInfo<MemberOrderEntity> memberOrderEntityPageInfo = new PageInfo<>(memberOrderEntities);
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(memberOrderEntityPageInfo);

        return generate;
    }

    @Override
    public HashMap<String, Integer> numInfo() {
        List<MemberOrderEntity> memberOrderEntities = memberOrderDao.selectList(null);
        HashMap<String, Integer> map = new HashMap<>();
        int sta0 = 0;
        int sta1 = 0;
        int sta2 = 0;
        int sta3 = 0;
        int sta4 = 0;
        int sta5 = 0;

        for (MemberOrderEntity entity : memberOrderEntities) {
            if (entity.getStatus()==0){
                sta0++;
            }
            if (entity.getStatus()==1){
                sta1++;
            }
            if (entity.getStatus()==2){
                sta2++;
            }
            if (entity.getStatus()==3){
                sta3++;
            }
            if (entity.getStatus()==4){
                sta4++;
            }
            if (entity.getStatus()==5){
                sta5++;
            }
        }
        map.put("sta0",sta0);
        map.put("sta1",sta1);
        map.put("sta2",sta2);
        map.put("sta3",sta3);
        map.put("sta4",sta4);
        map.put("sta5",sta5);
        map.put("all",memberOrderEntities.size());

        return map;
    }
}